﻿/*
 * Copyright (c) 2013 Lambert van Lieshout, YUMMO Software Development
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
*/

namespace System
{
    using System.Diagnostics;
    using System.Collections;
    using System.Reflection;
    using Yummo.BlackBox;

    /// <summary>
    /// Static holder for a BlackBox manager instance. Use SetManager to assign an manager.
    /// </summary>
    public static class Log
    {
        private static BlackBoxManager _manager;

        /// <summary>
        /// Set an BlackBoxManager instace.
        /// </summary>
        /// <param name="manager">BlackBoxManager</param>
        public static void SetManager(BlackBoxManager manager)
        {
            if (manager == null) throw new ArgumentNullException("manager", "Manager parameter cannot be NULL.");
            _manager = manager;
        }

        private static string GetCallingMethod()
        {
            System.Reflection.MethodBase m = new System.Diagnostics.StackTrace().GetFrame(2).GetMethod();
            return String.Concat(m.ReflectedType.FullName, ".", m.Name);
        }

        /// <summary>
        /// Write a event marked as critical.
        /// </summary>
        /// <param name="data">Data</param>
        /// <param name="parameters">Parameers</param>
        /// <param name="service">Namespace, class and method (formated as namespace.class.Method). When NULL it will be auto generated.</param>
        /// <param name="host">Host which triggers the event. When NULL it will be the running host.</param>
        [DebuggerStepThrough]
        public static void Critical(string data, IDictionary parameters = null, string service = null, string host = null)
        {
            if (_manager == null) throw new Exception("BlackBox manager not set. User SetManager to set manager.");
            if (service == null) service = GetCallingMethod();
            _manager.Write(EventTypes.Critical, data, parameters, service, host);
        }

        /// <summary>
        /// Write a event marked as debug.
        /// </summary>
        /// <param name="data">Data</param>
        /// <param name="parameters">Parameters as key (string) value (object) collection.</param>
        /// <param name="service">Namespace, class and method (formated as namespace.class.Method). When NULL it will be auto generated.</param>
        /// <param name="host">Host which triggers the event. When NULL it will be the running host.</param>
        [DebuggerStepThrough]
        public static void Debug(string data, IDictionary parameters = null, string service = null, string host = null)
        {
            if (_manager == null) throw new Exception("BlackBox manager not set. User SetManager to set manager.");
            if (service == null) service = GetCallingMethod();
            _manager.Write(EventTypes.Debug, data, parameters, service, host);
        }

        /// <summary>
        /// Write a event marked as error.
        /// </summary>
        /// <param name="data">Data</param>
        /// <param name="parameters">Parameters as key (string) value (object) collection.</param>
        /// <param name="service">Namespace, class and method (formated as namespace.class.Method). When NULL it will be auto generated.</param>
        /// <param name="host">Host which triggers the event. When NULL it will be the running host.</param>
        [DebuggerStepThrough]
        public static void Error(string data, IDictionary parameters = null, string service = null, string host = null)
        {
            if (_manager == null) throw new Exception("BlackBox manager not set. User SetManager to set manager.");
            if (service == null) service = GetCallingMethod();
            _manager.Write(EventTypes.Error, data, parameters, service, host);
        }

        /// <summary>
        /// Write a event marked as error.
        /// </summary>
        /// <param name="exception">Exception data used for the event message.</param>
        /// <param name="host">Host which triggers the event. When NULL it will be the running host.</param>
        [DebuggerStepThrough]
        public static void Error(Exception exception, string host = null)
        {
            if (_manager == null) throw new Exception("BlackBox manager not set. User SetManager to set manager.");
            if (exception.Source == null && exception.TargetSite == null) exception.Source = GetCallingMethod();
            _manager.Write(exception, host);
        }

        /// <summary>
        /// Write a event marked as trace.
        /// </summary>
        /// <param name="data">Data</param>
        /// <param name="parameters">Parameters as key (string) value (object) collection.</param>
        /// <param name="service">Namespace, class and method (formated as namespace.class.Method). When NULL it will be auto generated.</param>
        /// <param name="host">Host which triggers the event. When NULL it will be the running host.</param>
        [DebuggerStepThrough]
        public static void Trace(string data, IDictionary parameters = null, string service = null, string host = null)
        {
            if (_manager == null) throw new Exception("BlackBox manager not set. User SetManager to set manager.");
            if (service == null) service = GetCallingMethod();
            _manager.Write(EventTypes.Trace, data, parameters, service, host);
        }

        /// <summary>
        /// Write a event marked as warning.
        /// </summary>
        /// <param name="data">Data</param>
        /// <param name="parameters">Parameters as key (string) value (object) collection.</param>
        /// <param name="service">Namespace, class and method (formated as namespace.class.Method). When NULL it will be auto generated.</param>
        /// <param name="host">Host which triggers the event. When NULL it will be the running host.</param>
        [DebuggerStepThrough]
        public static void Warning(string data, IDictionary parameters = null, string service = null, string host = null)
        {
            if (_manager == null) throw new Exception("BlackBox manager not set. User SetManager to set manager.");
            if (service == null) service = GetCallingMethod();
            _manager.Write(EventTypes.Warning, data, parameters, service, host);
        }
    }
}